package cn.iocoder.yudao.module.resource.service.advertisement;

import cn.iocoder.yudao.module.resource.dal.dataobject.advertisement.ResourceAdvertisementDO;
import cn.iocoder.yudao.module.resource.dal.mysql.advertisement.ResourceAdvertisementMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

/**
 * 广告 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
@Slf4j
public class ResourceAdvertisementServiceImpl implements ResourceAdvertisementService {

    @Resource
    private ResourceAdvertisementMapper advertisementMapper;

    @Override
    public Long createAdvertisement(ResourceAdvertisementDO advertisementDO) {
        advertisementMapper.insert(advertisementDO);
        return advertisementDO.getId();
    }

    @Override
    public void updateAdvertisement(ResourceAdvertisementDO advertisementDO) {
        advertisementMapper.updateById(advertisementDO);
    }

    @Override
    public void deleteAdvertisement(Long id) {
        advertisementMapper.deleteById(id);
    }

    @Override
    public ResourceAdvertisementDO getAdvertisement(Long id) {
        return advertisementMapper.selectById(id);
    }

    @Override
    public List<ResourceAdvertisementDO> getAdvertisementListByAdvertiserId(Long advertiserId) {
        return advertisementMapper.selectListByAdvertiserId(advertiserId);
    }

    @Override
    public List<ResourceAdvertisementDO> getAdvertisementListByStatus(Integer status) {
        return advertisementMapper.selectListByStatus(status);
    }

    @Override
    public void incrementShowCount(Long id) {
        ResourceAdvertisementDO advertisement = advertisementMapper.selectById(id);
        if (advertisement != null) {
            ResourceAdvertisementDO updateObj = new ResourceAdvertisementDO();
            updateObj.setId(id);
            updateObj.setViewCount(advertisement.getViewCount() + 1);
            advertisementMapper.updateById(updateObj);
        }
    }

    @Override
    public void incrementClickCount(Long id) {
        ResourceAdvertisementDO advertisement = advertisementMapper.selectById(id);
        if (advertisement != null) {
            ResourceAdvertisementDO updateObj = new ResourceAdvertisementDO();
            updateObj.setId(id);
            updateObj.setClickCount(advertisement.getClickCount() + 1);
            advertisementMapper.updateById(updateObj);
        }
    }

}
